package com.hnichr.crm.biz.customer.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.hnichr.crm.common.customer.entity.CrmCustomer;
import com.hnichr.crm.common.customer.service.ICrmCustomerService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 客户管理Controller
 *
 * @author Cheng
 * @date 2022-11-22
 */
@Slf4j
@Tag(name = "客户管理Controller", description = "客户管理")
@RestController()
@RequestMapping("/customer/crmCustomer")
public class CrmCustomerController {

    @Autowired
    private ICrmCustomerService crmCustomerService;

    /**
     * 查询客户管理列表
     */
    @GetMapping("/list")
    @Operation(summary = "客户管理", description = "分页查询", method = "GET")
    public Result<Page<CrmCustomer>> list(Criteria criteria,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<CrmCustomer> page = crmCustomerService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 今日待跟进客户列表
     */
    @GetMapping("/listToday")
    @Operation(summary = "代办事项", description = "今日待跟进客户列表", method = "GET")
    public Result<Page<CrmCustomer>> listToday(Criteria criteria,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);

        String loginUsername = SecurityUtils.getLoginUsername();
        criteria.and()
                .addCriterion("nextTime", Operator.LE, DateUtil.endOfDay(new Date()))
                .addCriterion("ownerUserId", Operator.EQ, loginUsername)
                .end();

        Page<CrmCustomer> page = crmCustomerService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 查询客户公海池
     */
    @GetMapping("/public/list")
    @Operation(summary = "客户管理", description = "分页查询", method = "GET")
    public Result<Page<CrmCustomer>> publicList(Criteria criteria,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {

        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        List<String> deptCodes = crmCustomerService.getDeptCodes();
        criteria.and()
                .addCriterion("createDept", Operator.IN, deptCodes)
                .end();
        Page<CrmCustomer> page = crmCustomerService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    @Operation(summary = "客户管理-添加", description = "客户管理-添加", method = "POST")
    public Result<?> add(@RequestBody CrmCustomer crmCustomer) {
        crmCustomer.setStatement(false);
        crmCustomer.setBatchId(UUID.randomUUID().toString());
        if (StringUtils.isEmpty(crmCustomer.getOwnerUserId())) {
            crmCustomer.setOwnerUserId(SecurityUtils.getLoginUsername());
        }
        String msg = crmCustomerService.save(crmCustomer);
        if(StringUtils.isBlank(msg)){
            return Result.OK("添加成功！");
        }
        else{
            return Result.error(msg);
        }

    }

    /**
     * 编辑
     *
     * @param crmCustomer
     * @return
     */
    @Operation(summary = "客户管理-编辑", description = "客户管理-编辑", method = "PUT")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmCustomer crmCustomer) {
        String msg = crmCustomerService.update(crmCustomer);
        if(StringUtils.isBlank(msg)){
            return Result.OK("编辑成功！");
        }
        else{
            return Result.error(msg);
        }
    }

    /**
     * 变更交易状态
     *
     * @param crmCustomer
     * @return
     */
    @Operation(summary = "客户管理-变更交易状态", description = "客户管理-变更交易状态", method = "PUT")
    @PutMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody CrmCustomer crmCustomer) {
        CrmCustomer customer = crmCustomerService.getById(crmCustomer.getId());
        //0-未成交、1-已成交
        customer.setDealStatus(customer.getDealStatus() == 0 ? 1 : 0);
        crmCustomerService.update(customer);
        return Result.OK("变更交易状态成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @Operation(summary = "客户管理-通过id删除", description = "客户管理-通过id删除")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        crmCustomerService.delete(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @Operation(summary = "客户管理-批量删除", description = "客户管理-批量删除")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @Operation(summary = "客户管理-通过id查询", description = "客户管理-通过id查询")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CrmCustomer crmCustomer = crmCustomerService.getById(id);
        if (crmCustomer == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(crmCustomer);
    }


}
